
import pygame
from pygame.math import Vector2 as V2
Rect = pygame.Rect
from math import pi, tau, sin, cos
import cairo

def draw_polygon3(surface, color, pl, width):
    ctx = cairo.Context(surface)
    ctx.set_line_width(width)
    r, g, b, a= color
    ctx.move_to(*pl[-1])
    for p in pl:
        ctx.line_to(*p)
    ctx.set_source_rgba(r / 255, g / 255, b / 255, a / 255)
    ctx.stroke()

def fill_polygon3(surface, color, pl):
    ctx = cairo.Context(surface)
    r, g, b, a= color
    ctx.move_to(*pl[-1])
    for p in pl:
        ctx.line_to(*p)
    ctx.set_source_rgba(r / 255, g / 255, b / 255, a / 255)
    ctx.fill_preserve()


def rect_to_v(rect):
    return [V2(rect.topright), V2(rect.topleft), V2(rect.bottomleft), V2(rect.bottomright)]

def poly6(p, r):
    l = []
    for i in range(6):
        angle = tau / 6 * i
        l.append(p + r * V2(cos(angle), sin(angle)))
    return l


size = (100, 50)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(-25, 0, 50, 25)),  5)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(25, 0, 50, 25)),  5)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(75, 0, 50, 25)),  5)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(0, 25, 50, 25)),  5)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(50, 25, 50, 25)),  5)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture1.png")



size = (300, 173)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), poly6(V2(100, 86), 100),  10)
draw_polygon3(cs, (0, 0, 0, 255), poly6(V2(250, 0), 100),  10)
draw_polygon3(cs, (0, 0, 0, 255), poly6(V2(250, 173), 100),  10)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture2.png")


size = (100, 100)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(0, 0, 100, 100)),  10)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture3.png")

size = (100, 173)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), [V2(50, 0), V2(0, 86), V2(100, 86)],  10)
draw_polygon3(cs, (0, 0, 0, 255), [V2(50, 173), V2(0, 86), V2(100, 86)],  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(-50, 0, 200, 173)),  10)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture4.png")

size = (100, 173)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), [V2(50, 0), V2(0, 86), V2(100, 86)],  10)
draw_polygon3(cs, (0, 0, 0, 255), [V2(50, 173), V2(0, 86), V2(100, 86)],  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(-50, 0, 200, 173)),  10)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture4.png")

size = (200, 200)
cs = cairo.ImageSurface(cairo.FORMAT_ARGB32, *size)     # cairo_surface
fill_polygon3(cs, (255, 255, 255, 255), rect_to_v(Rect(0, 0, *size)))
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(0, 0, 100, 50)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(50, 50, 100, 50)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(100, 100, 100, 50)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(150, 150, 100, 50)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(-50, 150, 100, 50)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(0, 50, 50, 100)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(50, 100, 50, 100)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(100, 150, 50, 100)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(100, -50, 50, 100)),  10)
draw_polygon3(cs, (0, 0, 0, 255), rect_to_v(Rect(150, 0, 50, 100)),  10)
s = pygame.image.frombuffer(cs.get_data(), size, "RGBA")
pygame.image.save(s, "texture5.png")






